.text
.section .rodata
.LC0:
	.string	"Enter %d golf scores:\n"
.LC1:
	.string	"%d"
	.align 8
.LC2:
	.string	"the scores read in are as follows:"
.LC3:
	.string	"%5d"
	.align 8
.LC5:
	.string	"Sum of scores = %d, average = %.2f\n"
.LC7:
	.string	"That's a handicap of %0.f.\n"

.section .rodata
.LC4:
	.long	1077936128
	.align 4
.LC6:
	.long	1116733440
    .align 4

.text
.globl main
.type main @function
main:
    pushq %rbp
    movq %rsp, %rbp

    // 这里发现一个问题，竟然不能减24个字节，减了以后会报段错误，不太理解是为什么
    // subq $24, %rsp
    subq $32, %rsp
    // -4(%rbp) /* index */
    // -16(%rbp) /* score */
    // -20(%rbp) /* sum */
    // -24(%rbp) /* average */
    movl $0, -20(%rbp) /* sum */
    movl $3, %esi
    leaq .LC0(%rip), %rdi
    movl $0, %eax
    call printf

    movl $0, -4(%rbp) /* index */
    jmp .L2
.L1:
    leaq -16(%rbp) /* score */, %rdx
    movl -4(%rbp) /* index */, %eax
    cltq
    // 乘以4，因为int占4字节
    salq $2, %rax
    addq %rdx, %rax
    movq %rax, %rsi
    leaq .LC1(%rip), %rdi
    movl $0, %eax
    call scanf    
    addl $1, -4(%rbp) /* index */
.L2:
    movl -4(%rbp) /* index */, %eax
    movl $3, %edx
    cmpl %edx, %eax
    jl .L1

    // 输出变成puts，常规操作
    leaq .LC2(%rip), %rdi
    call puts

    movl $0, -4(%rbp) /* index */
    jmp .L4
.L3:
    leaq -16(%rbp) /* score */, %rdx
    movl -4(%rbp) /* index */, %eax
    cltq
    salq $2, %rax
    addq %rdx, %rax
    movl (%rax), %esi
    leaq .LC3(%rip), %rdi
    call printf
    addl $1, -4(%rbp) /* index */
.L4:
    cmpl $3, -4(%rbp) /* index */
    jl .L3

    movl $10, %edi
    call putchar

    movl $0, -4(%rbp) /* index */
    jmp .L6
.L5:
    leaq -16(%rbp) /* score */, %rdx
    movl -4(%rbp) /* index */, %eax
    cltq
    salq $2, %rax
    addq %rdx, %rax
    movl (%rax), %eax
    addl %eax, -20(%rbp) /* sum */
    addl $1, -4(%rbp) /* index */
.L6:
    cmpl $3, -4(%rbp) /* index */
    jl .L5

    pxor %xmm0, %xmm0
    cvtsi2ssl -20(%rbp) /* sum */, %xmm0
    movss .LC4(%rip), %xmm1
    divss %xmm1, %xmm0
    movss %xmm0, -24(%rbp) /* average */
    movl -20(%rbp) /* sum */, %esi
    pxor %xmm2, %xmm2
    cvtss2sd -24(%rbp) /* average */, %xmm2
    movq %xmm2, %xmm0
    leaq .LC5(%rip), %rdi
    movl $1, %eax
    call printf

    movss -24(%rbp) /* average */, %xmm0
    movss .LC6(%rip), %xmm1
    subss %xmm1, %xmm0
    pxor %xmm2, %xmm2
    cvtss2sd %xmm0, %xmm0
    leaq .LC7(%rip), %rdi
    movl $1, %eax
    call printf

    movl $0, %eax
    movq %rbp, %rsp
    popq %rbp
    ret